package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.common.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.UUID;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    @Override
    public ResponseResult upload(MultipartFile file) {
        //判断
        if(file == null || file.getSize() == 0){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //重新构造文件名称
        //aaab.jpg
        String oriFileName = file.getOriginalFilename();
        //生成文件名 sfljo83242l4dlkfjal
        String uuid = UUID.randomUUID().toString().replace("-","");
        String postfix = oriFileName.substring(oriFileName.lastIndexOf("."));
        String newFileName = uuid + postfix;
        //上传
        String url = null;
        try{
            url = fileStorageService.uploadImgFile("", newFileName, file.getInputStream());
        }catch (Exception e){
            log.error("上传图片异常",e);
            throw new CustomException(AppHttpCodeEnum.FILE_UPLOAD_ERROR);
        }
        //保存
        WmMaterial material = new WmMaterial();
        material.setType((short)1);
        material.setUrl(url);
        material.setUserId(WmThreadLocalUtil.get().getId());
        material.setIsCollection((short)0);
        material.setCreatedTime(new Date());
        save(material);
        return ResponseResult.okResult(material);
    }

    @Override
    public ResponseResult list(WmMaterialDto dto) {
        //校验
        dto.checkParam();

        IPage page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(WmMaterial::getUserId,WmThreadLocalUtil.get().getId());
        queryWrapper.eq(dto.getIsCollection() != null, WmMaterial::getIsCollection, dto.getIsCollection());

        queryWrapper.orderByDesc(WmMaterial::getCreatedTime);

        page(page,queryWrapper);

        ResponseResult result = new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
        result.setData(page.getRecords());

        return result;
    }
}
